package uo.ri.peristence.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import alb.util.jdbc.Jdbc;
import uo.ri.common.BusinessException;
import uo.ri.conf.Conf;
import uo.ri.peristence.AveriaGateway;

public class AveriaGatewayImpl implements AveriaGateway {

	private Connection connection;

	@Override
	public void setConnection(Connection con) {
		this.connection = con;
	}

	@Override
	public Map<String, Object> findById(Long id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Map<String, Object>> findAll() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Long save(Map<String, Object> invoice) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void delete(Long id) {
		// TODO Auto-generated method stub

	}

	@Override
	public void update(Map<String, Object> invoice) {
		// TODO Auto-generated method stub

	}

	@Override
	public String verificarEstadoAveria(Long id) throws SQLException {
		PreparedStatement pst = null;
		ResultSet rs = null;
		String status = null;
		try {
			pst = connection.prepareStatement(Conf.get("SQL_VERIFICAR_ESTADO_AVERIA"));
			
			pst.setLong(1, id);
			
			rs = pst.executeQuery();
			if (rs.next() == false) {
				status = rs.getString(1); 
			} 
			
		} finally {
			Jdbc.close(rs, pst);
		}
		return status;

	}

	@Override
	public void cambiarEstadoAveria(Long id, String status) throws SQLException {
		PreparedStatement pst = null;
		try {
			pst = connection.prepareStatement(Conf.get("SQL_ACTUALIZAR_ESTADO_AVERIA"));
			
			pst.setString(1, status);
			pst.setLong(2, id);

			pst.executeUpdate();
		} finally {
			Jdbc.close(pst);
		}
		
	}

	@Override
	public void vincularAveriaFactura(Long idAveria, Long idFactura)
			throws SQLException {
		PreparedStatement pst = null;
		try {
			pst = connection.prepareStatement(Conf.get("SQL_VINCULAR_AVERIA_FACTURA"));

			pst.setLong(1, idFactura);
			pst.setLong(2, idAveria);

			pst.executeUpdate();
				
		} finally {
			Jdbc.close(pst);
		}
		
	}

	@Override
	public Double consultaImporteManoObra(Long idAveria) throws SQLException {
		PreparedStatement pst = null;
		ResultSet rs = null;
		Double importeManoObra = null;
		try{
			pst = connection.prepareStatement(Conf.get("SQL_IMPORTE_MANO_OBRA"));
			pst.setLong(1, idAveria);
			
			rs = pst.executeQuery();
			if (rs.next()) {
				importeManoObra =  rs.getDouble(1);
			}
		}
		finally {
			Jdbc.close(rs,pst);
		}
		
		return importeManoObra;
		
	}

	@Override
	public double consultaImporteRepuestos(Long idAveria) throws SQLException {
		PreparedStatement pst = null;
		ResultSet rs = null;
		Double importeRepuestos = null;
		try{
			pst = connection.prepareStatement(Conf.get("SQL_IMPORTE_REPUESTOS"));
			pst.setLong(1, idAveria);
			
			rs = pst.executeQuery();
			if (rs.next()) {
				importeRepuestos = rs.getDouble(1); 
			} 		
	
			}	
		finally {
			Jdbc.close(rs,pst);
		}
		
		return importeRepuestos;
	}

	@Override
	public void actualizarImporteAveria(Long idAveria, double totalAveria)
			throws SQLException {
		PreparedStatement pst = null;
		
		try {
			pst = connection.prepareStatement(Conf.get("SQL_UPDATE_IMPORTE_AVERIA"));
			pst.setDouble(1, totalAveria);
			pst.setLong(2, idAveria);
			pst.executeUpdate();
		}	
		finally {
			Jdbc.close(pst);
		}
		
	}
	
	

}
